home *** CD-ROM | disk | FTP | other *** search
/ TPUG - Toronto PET Users Group / TPUG Users Group CD / TPUG Users Group CD.iso / AMIGA / AMICUS / AMICUS04.ADF / C / consoleIO.c < prev    next >
C/C++ Source or Header  |  1985-12-04  |  17KB  |  487 lines

  1.  
  2. /* console example program, including consoleIO.c at the end */
  3.  
  4. #include "exec/types.h"
  5. #include "exec/io.h"
  6. #include "exec/memory.h"
  7. #include "devices/console.h"
  8. #include "libraries/dos.h"
  9. #include "graphics/text.h"
  10. #include "libraries/diskfont.h"
  11. #include "intuition/intuition.h"
  12.  
  13. UBYTE escdata[] = { 0x9b, '@',  /* insert character */
  14.                     0x9b, 'A',  /* cursor up */
  15.                     0x9b, 'B',  /* cursor down */
  16.                     0x9b, 'C',  /* cursor left */
  17.                     0x9b, 'D',  /* cursor right */
  18.                     0x9b, 'E',  /* cursor next line */
  19.                     0x9b, 'F',  /* cursor prev line */
  20.                     0x9b, 'J',  /* erase to end of display */
  21.                     0x9b, 'K',  /* erase to end of line */
  22.                     0x9b, 'L',  /* insert line */
  23.                     0x9b, 'M',  /* delete line */
  24.                     0x9b, 'P',  /* delete character */
  25.                     0x9b, 'S',  /* scroll up */
  26.                     0x9b, 'T',  /* scroll down */
  27.                     0x1b, 'c',  /* reset to initial state */
  28.                     0x9b, 'q',  /* window status request */
  29.                     0x9b, 'n',  /* device status report */
  30.                     0x9b, 'p',  /* cursor on */
  31.                     0x9b, '0', ' ',  'p', /* cursor off */
  32.                     0x9b, '2', '0', 'h', /* set mode */
  33.                     0x9b, '2', '0', 'l', /* reset mode */
  34.                    };
  35.  
  36.         /* COVER A SELECTED SUBSET OF THE CONSOLE AVAILABLE FUNCTIONS */
  37.  
  38. #define INSERTCHARSTRING        &escdata[0] 
  39. #define CURSUPSTRING            &escdata[0+2] 
  40. #define CURSDOWNSTRING          &escdata[0+4] 
  41. #define CURSFWDSTRING           &escdata[0+6] 
  42. #define CURSBAKSTRING           &escdata[0+8] 
  43. #define CURSNEXTLINE            &escdata[0+10] 
  44. #define CURSPREVLINE            &escdata[0+12] 
  45. #define ERASEEODSTRING          &escdata[0+14] 
  46. #define ERASEEOLSTRING          &escdata[0+16] 
  47. #define INSERTLINESTRING        &escdata[0+18] 
  48. #define DELETELINESTRING        &escdata[0+20] 
  49. #define DELCHARSTRING           &escdata[0+22] 
  50. #define SCROLLUPSTRING          &escdata[0+24] 
  51. #define SCROLLDOWNSTRING        &escdata[0+26] 
  52. #define RESETINITSTRING         &escdata[0+28]
  53. #define WINDOWSTATSTRING        &escdata[0+30]
  54. #define DEVSTATSTRING           &escdata[0+32]
  55. #define CURSONSTRING            &escdata[0+34] 
  56. #define CURSOFFSTRING           &escdata[0+36] 
  57. #define SETMODESTRING           &escdata[0+40]
  58. #define RESETMODESTRING         &escdata[0+44]
  59.  
  60. #define BACKSPACE(r)            ConPutChar(r,0x08)
  61. #define TAB(r)                  ConPutChar(r,0x09)
  62. #define LINEFEED(r)             ConPutChar(r,0x0a)
  63. #define VERTICALTAB(r)          ConPutChar(r,0x0b)
  64. #define FORMFEED(r)             ConPutChar(r,0x0c)
  65. #define CR(r)                   ConPutChar(r,0x0d)
  66. #define SHIFTOUT(r)             ConPutChar(r,0x0e)
  67. #define SHIFTIN(r)              ConPutChar(r,0x0f)
  68. #define CLEARSCREEN(r)          ConPutChar(r,0x0c)
  69.  
  70. #define RESET(r)                ConWrite(r,RESETINITSTRING,2)
  71. #define INSERT(r)               ConWrite(r,INSERTCHARSTRING,2)
  72. #define CURSUP(r)               ConWrite(r,CURSUPSTRING,2)
  73. #define CURSDOWN(r)             ConWrite(r,CURSDOWNSTRING,2)
  74. #define CURSFWD(r)              ConWrite(r,CURSFWDSTRING,2)
  75. #define CURSBAK(r)              ConWrite(r,CURSBAKSTRING,2)
  76. #define CURSNEXTLN(r)           ConWrite(r,CURSNEXTLINE,2)
  77. #define CURSPREVLN(r)           ConWrite(r,CURSPREVLINE,2)
  78. #define ERASEEOD(r)             ConWrite(r,ERASEEODSTRING,2)
  79. #define ERASEEOL(r)             ConWrite(r,ERASEEOLSTRING,2)
  80. #define INSERTLINE(r)           ConWrite(r,INSERTLINESTRING,2)
  81. #define DELETELINE(r)           ConWrite(r,DELETELINESTRING,2)
  82. #define SCROLLUP(r)             ConWrite(r,SCROLLUPSTRING,2)
  83. #define SCROLLDOWN(r)           ConWrite(r,SCROLLDOWNSTRING,2)
  84. #define DEVICESTATUS(r)         ConWrite(r,DEVSTATSTRING,2)
  85. #define WINDOWSTATUS(r)         ConWrite(r,WINDOWSTATSTRING,2)
  86. #define DELCHAR(r)              ConWrite(r,DELCHARSTRING,2)
  87. #define CURSORON(r)             ConWrite(r,CURSONSTRING,2)
  88. #define CURSOROFF(r)            ConWrite(r,CURSOFFSTRING,4)
  89. #define SETMODE(r)              ConWrite(r,SETMODESTRING,4)
  90. #define RESETMODE(r)            ConWrite(r,RESETMODESTRING,4)
  91.  
  92. #define CloseConsole(r) CloseDevice(r)
  93.  
  94. ULONG DosBase;
  95. ULONG DiskfontBase;
  96. ULONG IntuitionBase;
  97. ULONG GfxBase;
  98.  
  99.  
  100. struct NewWindow nw = {
  101.         10, 10,        /* starting position (left,top) */
  102.         620,90,        /* width, height */
  103.         -1,-1,          /* detailpen, blockpen */
  104.         0,              /* flags for idcmp */
  105.   WINDOWDEPTH|WINDOWSIZING|WINDOWDRAG|SIMPLE_REFRESH|ACTIVATE|GIMMEZEROZERO,
  106.                         /* window gadget flags */
  107.         0,              /* pointer to 1st user gadget */
  108.         NULL,           /* pointer to user check */
  109.         "Console Test", /* title */
  110.         NULL,           /* pointer to window screen */
  111.         NULL,           /* pointer to super bitmap */
  112.         100,45,         /* min width, height */
  113.         640,200,        /* max width, height */
  114.         WBENCHSCREEN};
  115.  
  116. struct Window *w;
  117. struct RastPort *rp;
  118.         
  119. struct IOStdReq *consoleWriteMsg;   /* I/O request block pointer */
  120. struct Port *consoleWritePort;      /* a port at which to receive */    
  121. struct IOStdReq *consoleReadMsg;   /* I/O request block pointer */
  122. struct Port *consoleReadPort;      /* a port at which to receive */     
  123.         
  124. extern struct Port *CreatePort();
  125. extern struct IOStdReq *CreateStdIO();
  126.  
  127. char readstring[200];   /* provides a buffer even though using only one char */
  128.  
  129. main()
  130. {
  131.  
  132.         SHORT i;
  133.         SHORT status;
  134.         SHORT problem;
  135.         SHORT error;
  136.         problem = 0;
  137.  
  138.         if((DosBase = OpenLibrary("dos.library", 0)) == NULL) 
  139.                 { problem = 1; goto cleanup1; }
  140.         if((DiskfontBase=OpenLibrary("diskfont.library",0))==NULL) 
  141.                 { problem = 2; goto cleanup2; }
  142.         if((IntuitionBase=OpenLibrary("intuition.library",0))==NULL) 
  143.                 { problem = 3; goto cleanup3; }
  144.         if((GfxBase=OpenLibrary("graphics.library",0))==NULL) 
  145.                 { problem = 4; goto cleanup4; }
  146.  
  147.         consoleWritePort = CreatePort("my.con.write",0);
  148.         if(consoleWritePort == 0) 
  149.                 { problem = 5; goto cleanup5; }
  150.         consoleWriteMsg =  CreateStdIO(consoleWritePort);
  151.         if(consoleWritePort == 0)
  152.                 { problem = 6; goto cleanup6; }
  153.  
  154.         consoleReadPort = CreatePort("my.con.read",0);
  155.         if(consoleReadPort == 0) 
  156.                 { problem = 7; goto cleanup7; }
  157.         consoleReadMsg =  CreateStdIO(consoleReadPort);
  158.         if(consoleReadPort == 0) 
  159.                 { problem = 8; goto cleanup8; }
  160.  
  161.         w = (struct Window *)OpenWindow(&nw);   /* create a window */
  162.         if(w == NULL)
  163.                 { problem = 9; goto cleanup9; }
  164.  
  165.         rp = w->RPort;          /* establish its rastport for later */
  166.  
  167. /* ************************************************************************ */
  168. /* NOW, Begin using the actual console functions in consoleIO.c             */  
  169. /* ************************************************************************ */
  170.  
  171.         error = OpenConsole(consoleWriteMsg,consoleReadMsg,w);  
  172.         if(error != 0)  
  173.                 { problem = 10; goto cleanup10; }
  174.                 /* attach a console to this window, initialize
  175.                  * for both write and read */
  176.  
  177.         QueueRead(consoleReadMsg,&readstring[0]); /* tell console where to 
  178.                                                    * put a character that
  179.                                                    * it wants to give me
  180.                                                    * queue up first read */
  181.         ConWrite(consoleWriteMsg,"Hello, World\r\n",14);
  182.  
  183.         ConPutStr(consoleWriteMsg,"testing BACKSPACE");
  184.         for(i=0; i<10; i++)
  185.                 { BACKSPACE(consoleWriteMsg); Delay(30); }
  186.  
  187.         ConPutStr(consoleWriteMsg,"\r\n");
  188.  
  189.         ConPutStr(consoleWriteMsg,"testing TAB\r");
  190.         for(i=0; i<10; i++)
  191.                 { TAB(consoleWriteMsg); Delay(30); }
  192.  
  193.         ConPutStr(consoleWriteMsg,"\r\n");
  194.  
  195.         ConPutStr(consoleWriteMsg,"testing LINEFEED\r");
  196.         for(i=0; i<4; i++)
  197.                 { LINEFEED(consoleWriteMsg); Delay(30); }
  198.  
  199.         ConPutStr(consoleWriteMsg,"\r\n");
  200.  
  201.         ConPutStr(consoleWriteMsg,"testing VERTICALTAB\r");
  202.         for(i=0; i<4; i++)
  203.                 { VERTICALTAB(consoleWriteMsg); Delay(30); }
  204.  
  205.         ConPutStr(consoleWriteMsg,"\r\n");
  206.  
  207.         ConPutStr(consoleWriteMsg,"testing FORMFEED\r");
  208.         Delay(30);
  209.         for(i=0; i<2; i++)
  210.                 { FORMFEED(consoleWriteMsg); Delay(30); }
  211.  
  212.         ConPutStr(consoleWriteMsg,"\r\n");
  213.  
  214.         ConPutStr(consoleWriteMsg,"testing CR");
  215.         Delay(30);
  216.         CR(consoleWriteMsg);
  217.         Delay(60);
  218.         ConPutStr(consoleWriteMsg,"\r\n");
  219.  
  220.         ConPutStr(consoleWriteMsg,"testing INSERT\r");
  221.         for(i=0; i<4; i++)
  222.                 { INSERT(consoleWriteMsg); Delay(30); }
  223.  
  224.         ConPutStr(consoleWriteMsg,"\r\n");
  225.  
  226.         ConPutStr(consoleWriteMsg,"    testing DELCHAR\r");
  227.         CR(consoleWriteMsg);
  228.         for(i=0; i<4; i++)
  229.                 { DELCHAR(consoleWriteMsg); Delay(30); }
  230.  
  231.         ConPutStr(consoleWriteMsg,"\r\n");
  232.  
  233.         ConPutStr(consoleWriteMsg,"testing INSERTLINE\r");
  234.         CR(consoleWriteMsg);
  235.         for(i=0; i<3; i++)
  236.                 { INSERTLINE(consoleWriteMsg); Delay(30); }
  237.         ConPutStr(consoleWriteMsg,"\r\n");
  238.  
  239.         ConPutStr(consoleWriteMsg,"testing DELETELINE\r");
  240.         CR(consoleWriteMsg);
  241.         LINEFEED(consoleWriteMsg);
  242.         Delay(60);
  243.         for(i=0; i<4; i++)
  244.                 { DELETELINE(consoleWriteMsg); Delay(30); }
  245.         ConPutStr(consoleWriteMsg,"\r\n");
  246.  
  247.         ConPutStr(consoleWriteMsg,"testing CURSUP\r");
  248.         for(i=0; i<4; i++)
  249.                 { CURSUP(consoleWriteMsg); Delay(30); }
  250.  
  251.         ConPutStr(consoleWriteMsg,"\r\n");
  252.  
  253.         ConPutStr(consoleWriteMsg,"testing CURSDOWN\r");
  254.         for(i=0; i<4; i++)
  255.                 { CURSDOWN(consoleWriteMsg); Delay(30); }
  256.  
  257.         ConPutStr(consoleWriteMsg,"\r\n");
  258.  
  259.         ConPutStr(consoleWriteMsg,"testing CURSFWD\r");
  260.         for(i=0; i<4; i++)
  261.                 { CURSFWD(consoleWriteMsg); Delay(30); }
  262.  
  263.         ConPutStr(consoleWriteMsg,"\r\n");
  264.  
  265.         ConPutStr(consoleWriteMsg,"testing CURSBAK");
  266.         for(i=0; i<4; i++)
  267.                 { CURSBAK(consoleWriteMsg); Delay(30); }
  268.  
  269.         ConPutStr(consoleWriteMsg,"\r\n");
  270.  
  271.         ConPutStr(consoleWriteMsg,"testing CURSPREVLN");
  272.         for(i=0; i<4; i++)
  273.                 { CURSPREVLN(consoleWriteMsg); Delay(30); }
  274.  
  275.         ConPutStr(consoleWriteMsg,"\r\n");
  276.  
  277.         ConPutStr(consoleWriteMsg,"testing CURSNEXTLN");
  278.         for(i=0; i<4; i++)
  279.                 { CURSNEXTLN(consoleWriteMsg); Delay(30); }
  280.  
  281.         ConPutStr(consoleWriteMsg,"\r\n");
  282.  
  283.         ConPutStr(consoleWriteMsg,"testing ERASEEOD");
  284.         CURSPREVLN(consoleWriteMsg);
  285.         CURSPREVLN(consoleWriteMsg);
  286.         CURSPREVLN(consoleWriteMsg);
  287.         Delay(60);
  288.         for(i=0; i<4; i++)
  289.                 { ERASEEOD(consoleWriteMsg); Delay(30); }
  290.  
  291.         ConPutStr(consoleWriteMsg,"\r\n");
  292.  
  293.         ConPutStr(consoleWriteMsg,"testing ERASEEOL.junk");
  294.         CURSBAK(consoleWriteMsg);
  295.         CURSBAK(consoleWriteMsg);
  296.         CURSBAK(consoleWriteMsg);
  297.         CURSBAK(consoleWriteMsg);
  298.         CURSBAK(consoleWriteMsg);
  299.         Delay(60);
  300.         ERASEEOL(consoleWriteMsg); 
  301.         Delay(30);
  302.         ConPutStr(consoleWriteMsg,"\r\n");
  303.  
  304.         ConPutStr(consoleWriteMsg,"testing SCROLLUP");
  305.         for(i=0; i<4; i++)
  306.                 { SCROLLUP(consoleWriteMsg); Delay(30); }
  307.  
  308.         ConPutStr(consoleWriteMsg,"\r\n");
  309.         ConPutStr(consoleWriteMsg,"testing SCROLLDOWN");
  310.         ConPutStr(consoleWriteMsg,"\n\n\n");
  311.         for(i=0; i<4; i++)
  312.                 { SCROLLDOWN(consoleWriteMsg); Delay(30); }
  313.  
  314.         ConPutStr(consoleWriteMsg,"\r\n");
  315.  
  316.         ConPutStr(consoleWriteMsg,"testing CURSOROFF");
  317.         CURSOROFF(consoleWriteMsg); 
  318.         ConPutStr(consoleWriteMsg, "printed.with.cursor.off"); 
  319.         Delay(60);
  320.         ConPutStr(consoleWriteMsg,"\r\n");
  321.  
  322.         ConPutStr(consoleWriteMsg,"testing CURSORON");
  323.         CURSORON(consoleWriteMsg); Delay(30);
  324.  
  325.  
  326. /* ************************************************************************ */
  327.         Delay(120);     /* wait 2 seconds (120/60 ticks) */
  328.  
  329.         status = CheckIO(consoleReadMsg);       /* see if console read
  330.                                                  * anything, abort if not */
  331.         if(status == FALSE) AbortIO(consoleReadMsg);
  332.         WaitPort(consoleReadPort);      /* wait for abort to complete */
  333.         GetMsg(consoleReadPort);        /* and strip message from port */
  334.  
  335.         CloseConsole(consoleWriteMsg);
  336.    cleanup10:
  337.    cleanup9:
  338.         CloseWindow(w);
  339.    cleanup8:
  340.         DeleteStdIO(consoleReadMsg);
  341.    cleanup7:
  342.         DeletePort(consoleReadPort);
  343.    cleanup6:
  344.         DeleteStdIO(consoleWriteMsg);
  345.    cleanup5:
  346.         DeletePort(consoleWritePort);
  347.    cleanup4:
  348.         CloseLibrary(GfxBase);
  349.    cleanup3:
  350.         CloseLibrary(IntuitionBase);
  351.    cleanup2:
  352.         CloseLibrary(DiskfontBase);
  353.    cleanup1:
  354.         CloseLibrary(DosBase);
  355.         if(problem > 0) exit(problem+1000);
  356.         else
  357.                 return(0);
  358.  
  359. }       /* end of main() */
  360.  
  361.  
  362. /* Open a console device */
  363.  
  364.         /* this function returns a value of 0 if the console 
  365.          * device opened correctly and a nonzero value (the error
  366.          * returned from OpenDevice) if there was an error.
  367.          */
  368.         
  369.         int
  370. OpenConsole(writerequest,readrequest,window)
  371.         struct IOStdReq *writerequest;
  372.         struct IOStdReq *readrequest;
  373.         struct Window *window;
  374.         {       
  375.                 SHORT error;    
  376.                 writerequest->io_Data = (char *) window;
  377.                 writerequest->io_Length = sizeof(*window);
  378.                 error = OpenDevice("console.device", 0, writerequest, 0);
  379.                 readrequest->io_Device = writerequest->io_Device;
  380.                 readrequest->io_Unit   = writerequest->io_Unit;
  381.                         /* clone required parts of the request */
  382.                 return(error);  
  383.         }
  384.  
  385. /* Output a single character to a specified console */ 
  386.  
  387.         int
  388. ConPutChar(request,character)
  389.         struct IOStdReq *request;
  390.         char character;
  391.         {
  392.                 request->io_Command = CMD_WRITE;
  393.                 request->io_Data = &character;
  394.                 request->io_Length = 1;
  395.                 DoIO(request);
  396.                 /* command works because DoIO blocks until command is
  397.                  * done (otherwise pointer to the character could become
  398.                  * invalid in the meantime).
  399.                  */
  400.                 return(0);
  401.         }
  402.  
  403. /* Output a stream of known length to a console */ 
  404.  
  405.         int
  406. ConWrite(request,string,length)
  407.         struct IOStdReq *request;
  408.         char *string;
  409.         int length;
  410.         {
  411.                 request->io_Command = CMD_WRITE;
  412.                 request->io_Data = string;
  413.                 request->io_Length = length; 
  414.                 DoIO(request);
  415.                 /* command works because DoIO blocks until command is
  416.                  * done (otherwise pointer to string could become
  417.                  * invalid in the meantime).
  418.                  */
  419.                 return(0);
  420.         }
  421.  
  422. /* Output a NULL-terminated string of characters to a console */ 
  423.  
  424.         int
  425. ConPutStr(request,string)
  426.         struct IOStdReq *request;
  427.         char *string;
  428.         {
  429.                 request->io_Command = CMD_WRITE;
  430.                 request->io_Data = string;
  431.                 request->io_Length = -1;  /* tells console to end when it
  432.                                            * sees a terminating zero on
  433.                                            * the string. */
  434.                 DoIO(request);
  435.                 return(0);
  436.         }
  437.         
  438.         /* queue up a read request to a console, show where to
  439.          * put the character when ready to be returned.  Most
  440.          * efficient if this is called right after console is
  441.          * opened */
  442.  
  443.         int
  444. QueueRead(request,whereto)
  445.         struct IOStdReq *request;
  446.         char *whereto;
  447.         {
  448.                 request->io_Command = CMD_READ;
  449.                 request->io_Data = whereto;
  450.                 request->io_Length = 1;
  451.                 SendIO(request);
  452.                 return(0);
  453.         }
  454.  
  455.         /* see if there is a character to read.  If none, don't wait, 
  456.          * come back with a value of -1 */
  457.  
  458.         int 
  459. ConMayGetChar(request,requestPort, whereto)
  460.         struct IOStdReq *request;
  461.         char *whereto;
  462.         {
  463.                 register temp;
  464.  
  465.                 if ( GetMsg(requestPort) == NULL ) return(-1);
  466.                 temp = *whereto;
  467.                 QueueRead(request,whereto);
  468.                 return(temp);
  469.         }
  470.  
  471.         /* go and get a character; put the task to sleep if
  472.          * there isn't one present */
  473.  
  474.         UBYTE
  475. ConGetChar(consolePort,request,whereto)
  476.         struct IOStdReq *request; 
  477.         struct Port *consolePort;   
  478.         char *whereto;
  479.         {
  480.                 register temp;
  481.                 while((GetMsg(consolePort) == NULL)) WaitPort(consolePort);
  482.                 temp = *whereto;        /* get the character */
  483.                 QueueRead(request,whereto);
  484.                 return(temp);
  485.         }
  486.                         
  487.